package com.zsk.copy;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class MyRestore {

	public static void main(String[] args) throws IOException {
		String sourceDir = "E:\\eclipse-workspace\\cloud-platform_\\";
		String targetDir = "E:\\eclipse-workspace\\cloud-platform\\";
		File tagFile = new File(targetDir);
		tagFile.delete();
		tagFile.mkdirs();
		List<String> fileList = new ArrayList<String>();
		listFiles(sourceDir, fileList);
		for (String filePath : fileList) {
			if (filePath.contains("\\src") || filePath.contains("pom.xml") || filePath.length() > 0) {
				String newFilePath = filePath.replace(sourceDir, targetDir);
				newFilePath = newFilePath.substring(0, newFilePath.length() - 1);
				File tmpFile = new File(newFilePath);
				if (!tmpFile.getParentFile().isDirectory()) {
					tmpFile.getParentFile().mkdirs();
				}
				copyFile(new File(filePath), tmpFile);
			}
		}
		System.out.println("finsh");
	}

	public static void listFiles(String dir, List<String> fileList) {
		File file = new File(dir);
		File[] array = file.listFiles();

		for (int i = 0; i < array.length; i++) {
			if (array[i].isFile()) {
				try {
					fileList.add(array[i].getCanonicalPath());
				} catch (IOException e) {
					e.printStackTrace();
				}
			} else if (array[i].isDirectory()) {
				listFiles(array[i].getPath(), fileList);
			}
		}
	}

	/**
	 * 复制文件
	 * 
	 * @param fromFile
	 * @param toFile
	 */
	public static void copyFile(File fromFile, File toFile) {
		FileInputStream ins = null;
		FileOutputStream out = null;
		try {
			ins = new FileInputStream(fromFile);
			out = new FileOutputStream(toFile);
			byte[] b = new byte[1024];
			int n = 0;
			while ((n = ins.read(b)) != -1) {
				out.write(b, 0, n);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (ins != null) {
				try {
					ins.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (out != null) {
				try {
					out.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}

	}
}
